package orderoid.soap.business;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import orderoid.soap.Tool.DeserialisationTool;
import orderoid.soap.classeCommune.Restaurant;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.os.AsyncTask;
import android.util.Log;

public class webServiceRechercherRestaurants extends AsyncTask<String, Void, List<Restaurant>> {
	
	@SuppressWarnings("unchecked")
	@Override
	protected List<Restaurant> doInBackground(String... params) {
		List<Restaurant> restaurants = new ArrayList<Restaurant>();

		Log.e("Test", "doInBackground : WebService Recherche restaurant");

		SoapObject request = new SoapObject(
				orderoid.soap.constant.method.NAMESPACE,
				orderoid.soap.constant.method.METHOD_NAME_RECHERCHE_RESTAURANT);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		
		envelope.setOutputSoapObject(request);
		
		HttpTransportSE androidHttpTransport = new HttpTransportSE(
				orderoid.soap.constant.method.URL);

		try {
			androidHttpTransport.call(orderoid.soap.constant.method.SOAP_ACTION_RECHERCHE_RESTAURANT, envelope);
			Vector<SoapObject> soapObjectResp = (Vector<SoapObject>) envelope.getResponse();
			
			for ( SoapObject soC : soapObjectResp)
			{
				if ( soC.hasProperty("idRestaurant") ) 
				{
					restaurants.add(DeserialisationTool.deserialisationRestaurant(soC));
				}
			}
			
		} catch (Exception e) {
			Log.e("Erreur",e.getMessage());
			restaurants = null;
		}
		return restaurants;
	}

}
